package com.yxcz.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yxcz.dao.MenuDAO;
import com.yxcz.dao.RoleDAO;
import com.yxcz.model.AddMenuForm;
import com.yxcz.model.JsonMenu;
import com.yxcz.model.Menu;
import com.yxcz.model.Role;
import com.yxcz.model.UpdateMenuForm;
import com.yxcz.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {

	@Resource
	private MenuDAO menuDAO;
	@Resource
	private RoleDAO roleDAO;

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Menu> getMenuTree(int uid) {

		HashMap<String, Object> vars = new HashMap<>();
		vars.put("1", uid);

		return (ArrayList<Menu>) menuDAO.query(
				"select DISTINCT menu from User u join u.roles role join role.menus menu where u.id=?1  order by menu.pPenu.id,menu.sequence",
				vars);
	}

	/**
	 * 查路由
	 */
	@Override
	public Stack<Menu> getRouter(String servletPath) {

		HashMap<String, Object> vars = new HashMap<>();
		vars.put("1", servletPath);
		@SuppressWarnings("unchecked")
		ArrayList<Menu> menus = (ArrayList<Menu>) menuDAO.query("from Menu menu where menu.url=?1", vars);
		Stack<Menu> mstack = new Stack<>();

		if (menus.size() != 0) {
			Menu menu = menus.get(0);
			while (menu != null) {
				mstack.push(menu);
				menu = menu.getpPenu();
			}
		}
		return mstack;
	}

	/**
	 * 列出所有菜单
	 */
	@Override
	public ArrayList<JsonMenu> list() {
		HashMap<String, Object> vars = new HashMap<>();
		@SuppressWarnings("unchecked")
		ArrayList<Object[]> menus = (ArrayList<Object[]>) menuDAO
				.query("select menu.id,menu.pPenu.id,menu.name from Menu menu", vars);
		ArrayList<JsonMenu> jsontree = new ArrayList<>();
		for (Object[] object : menus) {
			JsonMenu jsonmenu = new JsonMenu();
			jsonmenu.setId((int) object[0]);
			jsonmenu.setpId(object[1] == null ? 0 : (int) object[1]);
			jsonmenu.setName((String) object[2]);
			jsontree.add(jsonmenu);
		}

		return jsontree;
	}

	/**
	 * 分配权限到权限组
	 */
	@Override
	public boolean setMenu(int rid, Integer[] menu_list) {

		Role role = roleDAO.find(rid);
		if (role == null)
			return false;

		Set<Menu> menus = new HashSet<>();
		for (int i : menu_list) {
			Menu menu = new Menu();
			menu.setId(i);
			menus.add(menu);
		}
		role.setMenus(menus);
		roleDAO.save(role);
		return true;

	}

	/**
	 * 通过ID查找菜单实体
	 */
	@Override
	public Menu find(int id) {

		return menuDAO.find(id);
	}

	/**
	 * 更新菜单信息
	 */
	@Override
	public void update(UpdateMenuForm form) {
		Menu menu = menuDAO.find(form.getId());
		menu.setName(form.getName());
		menu.setIcon(form.getIcon());
		menu.setRemark(form.getRemark());
		menu.setSequence(form.getSequence());
		menu.setUrl(form.getUrl());
		menu.setStatus(form.isStatus());
		menuDAO.save(menu);
	}

	/**
	 * 删除菜单
	 */
	@Override
	public void delete(Integer id) {
		Menu menu = find(id);
		Menu pmenu = menu.getpPenu();
		if (pmenu != null && pmenu.getChildMenu().size()-1 == 0) {
			pmenu.setIsleave(true);
			menuDAO.save(pmenu);
		}
		menuDAO.delete(menu);
	}

	/**
	 * 增加菜单
	 */
	@Override
	public boolean add(AddMenuForm form) {
		Menu menu = new Menu();
		Menu pMenu = menuDAO.find(form.getPid());
		if(pMenu==null)
		{
			
			menu.setLevel(1);
		}
		else
		{
			menu.setpPenu(pMenu);
			menu.setLevel(pMenu.getLevel()+1);
			pMenu.setIsleave(false);
		}
		
		menu.setIsleave(true);
		menu.setIcon(form.getIcon());
		menu.setSequence(form.getSequence());
		menu.setName(form.getName());
		menu.setRemark(form.getRemark());
		menu.setStatus(form.isStatus());
		menu.setUrl(form.getUrl());
		
		
		try {
			menuDAO.add(menu);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

}
